<?php
/** @noinspection PhpUnused */
declare(strict_types=1);

namespace app\shop\controller;

use app\admin\model\shop\goods\Goods;
use app\shop\exception\FailException;
use app\shop\exception\ParamsErrorException;
use app\shop\logic\GoodsLogic;
use app\shop\validate\GoodsValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Cache;
use think\facade\Db;
use think\response\Json;

/**
 * @property GoodsLogic $logic
 */
class GoodsController extends BaseController
{
    protected string $validateClassName = GoodsValidate::class;

    protected array $noNeedLogin = ['index', 'detail', 'classify', 'topClassify', 'classifyDetail','getAllgoods'];

    public function __construct(GoodsLogic $goodsLogic)
    {
        parent::__construct();

        $this->logic = $goodsLogic;
    }

    /**
     * 商品列表
     * @return Json
     * @throws ParamsErrorException
     */
    public function index(): Json
    {
        $params = $this->getInput();

        $this->validateData($params, 'list');

        $result = $this->logic->list($params);

        return $this->success($result);
    }

    /**
     * 商品详情
     * @return Json
     * @throws ParamsErrorException
     * @throws FailException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function detail(): Json
    {
        $params = $this->getInput(true);

        $this->validateData($params, 'detail');

        $result = $this->logic->detail($params);

        return $this->success($result);
    }

    /**
     * 商品分类
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws ParamsErrorException
     */
    public function classify(): Json
    {
        $params = $this->getInput();

        $this->validateData($params, 'classify');

        $result = $this->logic->getAllClassify($params);

        return $this->success($result);
    }

    /**
     * 商品一级分类
     * @return Json
     */
    public function topClassify(): Json
    {
        $result = $this->logic->topClassify();

        return $this->success($result);
    }

    /**
     * 分类详情
     * @return Json
     * @throws ParamsErrorException
     */
    public function classifyDetail(): Json
    {
        $params = $this->getInput();

        $this->validateData($params, 'classify_detail');

        $result = $this->logic->getGoodsListByClassify($params);

        return $this->success($result);
    }

    /**
     * 商品服务与承诺
     * @return Json
     * @throws ParamsErrorException
     */
    public function service(): Json
    {
        $params = $this->getInput();

        $this->validateData($params, 'service');

        $result = $this->logic->getServiceList($params);

        return $this->success($result);
    }

    /**
     * 分享
     * @return Json
     * @throws FailException
     * @throws ParamsErrorException
     */
    public function share(): Json
    {
        $params = $this->getInput(true);

        $this->validateData($params, 'share');

        $result = $this->logic->shareInfo($params);

        return $this->success($result);
    }

    /**
     * 获取全部商品
     * @return void
     */
    public function getAllgoods()
    {
        $return = Cache::get('getAllGoods');
        $return = false;
        if(!$return){
            $return = [];
            $goodsModel = new Goods();
            $types = Db::name('shop_goods_classify')->where('pid',0)->where('status',1)->select()->toArray();
            if($types){
                foreach ($types as $k=>$type){
                    $return[$k] = ['classify'=>$type];
                    $classifyids = Db::name('shop_goods_classify')->where('pid',$type['id'])->column('id');
                    if(isset($classifyids)){
                        foreach ($classifyids as $key=>$classifyid){
                            $classifyids[$key] = $type['id'].'-'.$classifyid;
                        }
                        $list = $goodsModel->where('classify_id','in',$classifyids)->where('status',1)->select()->toArray();
                        $return[$k]['classify']['goods'] = $list;
                    }
                }
            }
            if($return){
                Cache::set('getAllGoods', $return,3600);
            }else{
                $return = [];
            }
        }
        return $this->success($return);
    }
}
